package com.colorfruit.admin.control.controller;

import com.colorfruit.admin.business.service.PayBusinessOrderService;
import com.colorfruit.admin.pojo.constant.CommonConstant;
import com.colorfruit.framework.common.util.CommonUtil;
import com.colorfruit.framework.common.util.DataValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    PayBusinessOrderService payBusinessOrderService;

    @RequestMapping("/wxPayCallBack")
    public void wxPayCallBack(HttpServletRequest request, HttpServletResponse response) {
        try {
            log.info("# [IN] [controller] wxPayCallBack param:");
            InputStream inStream;
            inStream = request.getInputStream();
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            outSteam.close();
            inStream.close();
            String result = new String(outSteam.toByteArray(), "utf-8");
            Map<String, Object> map = CommonUtil.xmlToMap(result);
            String mapString = CommonUtil.mapToString(map);
            //map转对象
//            WxPayCallBackRequest wxPayCallBackRequest = JSON.parseObject(JSON.toJSONString(map), WxPayCallBackRequest.class);
            log.info("微信支付回调处理前的数据 = {}, map= {}",result, mapString);
            if (DataValidateUtil.verifyString((String)map.get(CommonConstant.RETURN_CODE), (String)map.get(CommonConstant.RESULT_CODE))
                    && CommonConstant.SUCCESS.equalsIgnoreCase((String)map.get(CommonConstant.RETURN_CODE))
                    && CommonConstant.SUCCESS.equalsIgnoreCase((String)map.get(CommonConstant.RESULT_CODE))) {
                //支付成功回调业务处理
                payBusinessOrderService.wxPayCallBack(map);
                // 以下字段在return_code 和result_code都为SUCCESS的时候有返回
                Map<String, String> prepayParams = new HashMap<String, String>();
                prepayParams.put(CommonConstant.RETURN_CODE, CommonConstant.SUCCESS);
                prepayParams.put(CommonConstant.RETURN_MSG, CommonConstant.SUCCESS);
                byte[] byteXmls = CommonUtil.callMapToXML(prepayParams);
                String xml = new String(byteXmls);
                PrintWriter pw = response.getWriter();
                //System.out.println("支付完成");
                pw.println(xml);
                pw.flush();
                pw.close();
                log.info("# [OUT] [controller] wxPayCallBack SUCCESS");
            }
        } catch (Exception e) {
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
        }
    }

}